package top.kpromise.common.base;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.Optional;

public abstract class BaseServiceImpl<T, Repository extends BaseRepository<T>> implements BaseService<T> {

    public final Repository repository;

    protected BaseServiceImpl(Repository repository) {
        this.repository = repository;
    }

    @Override
    @NonNull
    public List<T> findAll() {
        return repository.findAll();
    }

    @Override
    @NonNull
    public List<T> findAll(@NonNull Sort sort) {
        return repository.findAll(sort);
    }

    @Override
    @NonNull
    public List<T> findAllById(@NonNull Iterable<Long> iterable) {
        return repository.findAllById(iterable);
    }

    @Override
    @NonNull
    public <S extends T> List<S> saveAll(@NonNull Iterable<S> iterable) {
        return repository.saveAll(iterable);
    }

    @Override
    public void flush() {
        repository.flush();
    }

    @Override
    @NonNull
    public <S extends T> S saveAndFlush(@NonNull S s) {
        return repository.saveAndFlush(s);
    }

    @Override
    public void deleteInBatch(@NonNull Iterable<T> iterable) {
        repository.deleteInBatch(iterable);
    }

    @Override
    public void deleteAllInBatch() {
        repository.deleteAllInBatch();
    }

    @Override
    @NonNull
    public T getOne(@NonNull Long aLong) {
        return repository.getOne(aLong);
    }

    @Override
    @NonNull
    public <S extends T> List<S> findAll(@NonNull Example<S> example) {
        return repository.findAll(example);
    }

    @Override
    @NonNull
    public <S extends T> List<S> findAll(@NonNull Example<S> example, @NonNull Sort sort) {
        return repository.findAll(example, sort);
    }

    @Override
    @NonNull
    public Page<T> findAll(@NonNull Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    @NonNull
    public <S extends T> S save(@NonNull S s) {
        return repository.save(s);
    }

    @Override
    @NonNull
    public Optional<T> findById(@NonNull Long aLong) {
        return repository.findById(aLong);
    }

    @Override
    public boolean existsById(@NonNull Long aLong) {
        return repository.existsById(aLong);
    }

    @Override
    public long count() {
        return repository.count();
    }

    @Override
    public void deleteById(@NonNull Long aLong) {
        repository.deleteById(aLong);
    }

    @Override
    public void delete(@NonNull T t) {
        repository.delete(t);
    }

    @Override
    public void deleteAll(@NonNull Iterable<? extends T> iterable) {
        repository.deleteAll(iterable);
    }

    @Override
    public void deleteAll() {
        repository.deleteAll();
    }

    @Override
    @NonNull
    public <S extends T> Optional<S> findOne(@NonNull Example<S> example) {
        return repository.findOne(example);
    }

    @Override
    @NonNull
    public <S extends T> Page<S> findAll(@NonNull Example<S> example, @NonNull Pageable pageable) {
        return repository.findAll(example, pageable);
    }

    @Override
    public <S extends T> long count(@NonNull Example<S> example) {
        return repository.count(example);
    }

    @Override
    public <S extends T> boolean exists(@NonNull Example<S> example) {
        return repository.exists(example);
    }
}
